package com.xnj.testweb.admin.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PdfGenerator {

    // 定义中文字体
    private static Font chineseFont;

    static {
        try {
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            chineseFont = new Font(bf, 12, Font.NORMAL);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void generatePdf(String filePath, List<DataObject> dataList) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter.getInstance(document, new FileOutputStream(filePath));
            document.open();

            // 创建表格，列数为3
            PdfPTable table = new PdfPTable(3);
            table.setWidthPercentage(100);

            // 添加表头
            addTableHeader(table);

            // 添加数据行
            for (DataObject data : dataList) {
                addRow(table, data);
            }

            document.add(table);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }

    private static void addTableHeader(PdfPTable table) {
        PdfPCell cell = new PdfPCell(new Phrase("样品编号", chineseFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("货区", chineseFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("时间", chineseFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }

    private static void addRow(PdfPTable table, DataObject data) {
        table.addCell(new Phrase(data.getName(), chineseFont));
        table.addCell(new Phrase(data.getAge(), chineseFont));
        table.addCell(new Phrase(formatDateToString(data.getAddress()), chineseFont));
    }

    public static void main(String[] args) {
        // 示例数据
        List<DataObject> dataList = List.of(
                new DataObject("YN2024/00440201", "隔板货架区", new Date()),
                new DataObject("YN2024/00440202", "料箱存储区矮货架", new Date()),
                new DataObject("YN2024/00440203", "料箱存储区高货架", new Date())
        );

        // 生成PDF文件
        generatePdf("D:\\downloadAll\\pdf\\output.pdf", dataList);
    }

    public static String formatDateToString(Date date) {
        // 定义日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 格式化 Date 对象
        return sdf.format(date);
    }
}

class DataObject {

    private String name;

    private String age;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date address;

    public DataObject(String name, String age, Date address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }

    public Date getAddress() {
        return address;
    }
}